import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class Baxterpump {
	private
		double conc;
		double rate;
		double vol;
		double dose;
		double volume;
		double drugamount;
		double dilutent;
		Time tm;
		Units ut;
		int m;
		static int next_pres = 1;
		
		public static class Mode {
			public static final int VR = 1;
			public static final int VT = 2;
			public static final int PI = 3;
			public static final int PB = 4;	
			public static List<Integer> Modes = Arrays.asList(VR, VT, PI, PB);
		}
		
		UserSimulator user;
	public
		Baxterpump(UserSimulator u) {
			user = u;
			init();
		}
		void init() {
			conc = -1;
			rate = -1;
			vol = -1;
			dose = -1;
			volume = -1;
			ut = null;
			drugamount = -1;
			dilutent = -1;
			m = -1;
			tm = new Time(-1, -1);
		}
		boolean run() {
			m = user.getMode();
			System.out.println("Baxter mode " + m );
			if (m == Mode.VR) {
				return volumerate();
			
			}
			else if (m == Mode.VT) {
				return volumetime();
				
			} 
			else if (m == Mode.PI) {
				return patientindependant();
				
			} else {
				return false;
			} 
			/*
			else {
				return patientweight();		
			}
			*/
		}
	
		boolean volumerate() {
			System.out.println("Pump Mode VR");
			rate = user.getRate();
			System.out.println(rate);
			if(rate == -1) {
				return false;
			}
			conc = user.getConc();
			System.out.println(conc);
			if(conc == -1) {
				return false;
			}
			volume = user.getVolume();
			System.out.println(volume);
			if(volume == -1) {
				return false;
			}
			return true;
		}
		
		boolean volumetime() {
			System.out.println("Pump Mode VT");
			conc = user.getConc();
			System.out.println(conc);
			if(conc == -1) {
				return false;
			}
			volume = user.getVolume();
			System.out.println(volume);
			if(volume == -1) {
				return false;
			}
			tm = user.getTime();
			System.out.println(tm.toString());
			if(tm.hours == -1) {
				return false;
			}
			return true;
		}
		
		boolean patientweight() {
			System.out.println("Pump Mode PI");
			return false;			
		}
		
		boolean patientindependant() {
			System.out.println("Pump Mode PI");
			ut = user.getUnits();
			if(ut == null) {
				return false;
			}
			conc = user.getConc();
			if(conc == -1) {
				return false;
			}
			drugamount = user.get_drug_amount();
			if(drugamount == -1) {
				return false;
			}
			dilutent = user.get_dilutent_volume();
			if(dilutent == -1) {
				return false;
			}
			dose = user.getDose();
			if(dose == -1) {
				return false;
			}
			volume = user.getVolume();
			if(volume == -1) {
				return false;
			}
			return true;
		}
				
		boolean gen_pres() {
			
			// must check for valid values here
			if (m == Mode.PI) {
				//rate must be computed, rate = dose/conc
				rate = (dose/conc)*ut.tomcghr();
			}
			else if (m == Mode.PB) {
				
			}
			else if (m == Mode.VT){
				//rate = volume in ml/time in mins
				rate = volume/tm.toMinutes();
				
			}
			try {
				String name = new String("/home/sid/prescription/" + "prescription_" + next_pres + user.Prestype());
			    BufferedWriter out = new BufferedWriter(new FileWriter(name));
			    out.write("****Input Prescription***");
			    out.newLine();
			    out.write(user.org_prescription());
			    out.write("****Error Prescription***");
			    out.newLine();
			    out.write(user.err_prescription());
			    out.write("***Pump Output***");
			    out.newLine();
			    out.write("Volume: " + volume);
			    out.newLine();
			    out.write("Rate: " + rate);    
			    out.newLine();
			    out.write("Conc: " + conc);
			    out.newLine();
			    out.close();
			    next_pres++;
			    return true;
			} catch (IOException e) {
			}
			return false;
		}

}
